package Athena::Engine;

use strict;

use Athena::Connection;

sub new {
  # Get the class name
  my $stInvocant = shift(@_);
  my $stClass = ref($stInvocant) || $stInvocant;
  
  # Create the current instance and bless into class
  my $fSelf = { };
  bless($fSelf,$stClass);

  my $cConn = shift(@_);
  my %hArgs = ( @_ );
  my $stArgs = \%hArgs;
  
  if (defined($cConn)) {
    $fSelf->{connection} = $cConn || undef;
  }
  else {
    $fSelf = undef;
  }
  return $fSelf;
}

sub int_to_yn {
  my $rSelf = shift(@_);
  my $stTest = shift(@_);
  if (scalar($stTest)) {
    return '+';
  }
  return '<font color="#800000">-</font>';
}

sub refresh {
  my $rSelf = shift(@_);
  my $cConn = $rSelf->{connection};
  my $stStatus = $cConn->httpGet($cConn->getUrl() . '/status/engine/?%S');
  my @arStats = split(/\n/, $stStatus);
  my $stStat;
  foreach $stStat (@arStats) {
    $rSelf->_deserialize($stStat);
  }
}

sub _deserialize {
  my $rSelf = shift(@_);
  my $stStat = shift(@_);
  my ($key, $val, $subval) = split(/: /, $stStat);

  if ($key eq "phys") {
    $rSelf->{stats}->{phys}->{$val} = $subval;
  }
  elsif ($key eq "farm") {
    $rSelf->{stats}->{farm}->{$val} = $subval;
  }
  else {
    $rSelf->{stats}->{$key} = $val;
  }
}

1;
